/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhuangfei.ohos_timetableview.slice;

import com.zhuangfei.ohos_timetableview.ResourceTable;
import com.zhuangfei.ohos_timetableview.base.BaseAbilitySlice;
import com.zhuangfei.ohos_timetableview.model.MySubject;
import com.zhuangfei.ohos_timetableview.model.SubjectRepertory;
import com.zhuangfei.ohos_timetableview.spinner.MaterialSpinner;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.listener.OnDateBuildAapter;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

import java.util.List;

/**
 * DateSlice
 *
 * @since 2021-03-29
 */
public class DateSlice extends BaseAbilitySlice {
    private static final String[] SPIN_MUNE = {"隐藏日期栏", "显示日期栏", "自定义日期栏", "恢复默认日期栏"};
    TimetableView mTimetableView;
    Intent intents;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_date);
        initViews();
        showPopmenu();
        intents = intent;
    }

    private void showPopmenu() {
        MaterialSpinner spinner = (MaterialSpinner) findComponentById(ResourceTable.Id_id_more);
        spinner.setOnItemSelectedListener((MaterialSpinner.OnItemSelectedListener<String>) (
                view, position, id, item) -> {
            selectLayout(position);
            spinner.setShowFalse();
        });
        spinner.setItems(SPIN_MUNE);
        spinner.setOnNothingSelectedListener(spinner1 -> spinner1.setShowFalse());
    }

    private void selectLayout(int position) {
        switch (position) {
            case 0:
                hideDateView();
                break;
            case 1:
                showDateView();
                break;
            case 2:
                customDateView();
                break;
            case 3:
                cancelCustomDateView();
                break;
            default:
                break;
        }
    }

    private void initViews() {
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);

        List<MySubject> mySubjects = SubjectRepertory.loadDefaultSubjects();
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .showView();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    /**
     * 隐藏日期栏
     */
    protected void hideDateView() {
        mTimetableView.hideDateView();
    }

    /**
     * 显示日期栏
     */
    protected void showDateView() {
        mTimetableView.showDateView();
    }

    /**
     * 自定义日期栏
     * 该段代码有点长，但是很好懂，仔细看看会有收获的，嘻嘻
     */
    protected void customDateView() {
        mTimetableView.callback(
                new OnDateBuildAapter() {
                    @Override
                    public Component onBuildDayLayout(LayoutScatter mInflate, int pos, int width, int height) {
                        int newHeight = 100;
                        Component view = mInflate.parse(ResourceTable.Layout_item_custom_dateview, null, true);
                        Text dayTextView = (Text) view.findComponentById(ResourceTable.Id_id_week_day);
                        dayTextView.setText(dateArray[pos]);
                        layouts[pos] = (DirectionalLayout) view.findComponentById(ResourceTable.Id_id_week_layout);

                        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(width, newHeight);
                        layouts[pos].setLayoutConfig(lp);
                        return view;
                    }

                    @Override
                    public Component onBuildMonthLayout(LayoutScatter scatter, int width, int height) {
                        int newHeight = 100;
                        Component first = scatter.parse(ResourceTable.Layout_item_custom_dateview_first, null, true);
                        /**
                         * 月份设置
                         */
                        textViews[0] = (Text) first.findComponentById(ResourceTable.Id_id_week_month);
                        layouts[0] = null;

                        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(width, newHeight);

                        int month = Integer.parseInt(weekDates.get(0));
                        first.setLayoutConfig(lp);
                        textViews[0].setText(month + "\n月");
                        return first;

                    }
                })
                .updateDateView();
    }

    /**
     * 恢复默认日期栏
     */
    protected void cancelCustomDateView() {
        mTimetableView.callback((ISchedule.OnDateBuildListener) null)
                .updateDateView();
    }
}
